package com.sam.spring.chapter5;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sam.spring.chapter5.service.IHelloWorldService;
import com.sam.spring.chapter5.service.IIntroductionService;

public class AOPTest {
	@Test
	public void testHelloWorld() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"chapter5/helloworld.xml");
		IHelloWorldService helloHelloWorldService = ctx.getBean(
				"helloWorldService", IHelloWorldService.class);
		helloHelloWorldService.sayHello();
	}

	@Test
	public void testSchemaBeforeAdvice() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"chapter5/advice.xml");
		IHelloWorldService helloworldService = ctx.getBean("helloWorldService",
				IHelloWorldService.class);
		helloworldService.sayBefore("before");
	}

	@Test
	public void testSchemaAfterReturningAdvice() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"chapter5/advice.xml");
		IHelloWorldService helloworldService = ctx.getBean("helloWorldService",
				IHelloWorldService.class);
		helloworldService.sayAfterReturning();
	}

	@Test(expected = RuntimeException.class)
	public void testSchemaAfterThrowingAdvice() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"chapter5/advice.xml");
		IHelloWorldService helloworldService = ctx.getBean("helloWorldService",
				IHelloWorldService.class);
		helloworldService.sayAfterThrowing();
	}

	@Test(expected = RuntimeException.class)
	public void testSchemaAfterFinallyAdvice() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"chapter5/advice.xml");
		IHelloWorldService helloworldService = ctx.getBean("helloWorldService",
				IHelloWorldService.class);
		helloworldService.sayAfterFinally();
	}

	@Test
	public void testSchemaAroundAdvice() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"chapter5/advice.xml");
		IHelloWorldService helloworldService = ctx.getBean("helloWorldService",
				IHelloWorldService.class);
		helloworldService.sayAround("haha");
	}

	@Test
	public void testSchemaIntroduction() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"chapter5/advice.xml");
		IIntroductionService introductionService = ctx.getBean(
				"helloWorldService", IIntroductionService.class);
		introductionService.induct();
	}

	@Test
	public void testSchemaAdvisor() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"chapter5/advice.xml");
		IHelloWorldService helloworldService = ctx.getBean("helloWorldService",
				IHelloWorldService.class);
		helloworldService.sayAdvisorBefore("haha");
	}
}
